$(window).ready(function(){
	function getSignatureSucFn(data){
		if(data.code == "0000"){
			var config = data.data;
			if(config != undefined && config != null){
				wx.config({
					debug : false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId : config.AppId, // 必填，appID公众号的唯一标识
					timestamp : config.Timestamp, // 必填，生成签名的时间戳
					nonceStr : config.NonceStr, // 必填，生成签名的随机串
					signature : config.Signature,// 必填，签名，见附录1
					jsApiList : [ 'onMenuShareAppMessage','onMenuShareTimeline' ]
				});

				wx.ready(function() {
					wx.onMenuShareAppMessage({//分享到好友
						title: 'e积分，来签到就可享受豪华汽车大保健啦！',
						desc: 'e车险保福利大放送，每天签到兑换不同礼品，还有各种小活动等您来拿奖品哦~',
						link: 'http://wx.ydche.com/eInsurance/signTem.html',
						imgUrl: 'http://wx.ydche.com/eInsurance/images/signshare.jpg',
						type: 'link',
						dataUrl: '',
						success: function () { 
							
						},
						cancel: function () { 
						   
						}
					});
					wx.onMenuShareTimeline({//分享到朋友圈
						title: 'e积分，来签到就可享受豪华汽车大保健啦！',
						link: 'http://wx.ydche.com/eInsurance/signTem.html',
						imgUrl: 'http://wx.ydche.com/eInsurance/images/signshare.jpg',
						trigger: function (res) {
							
						},
						success: function (res) { 
							
						},
						cancel: function (res) { 
							
						},
						fail: function (res) {
							
						}
					});
				})
			}
		}
	}

	function getRankSucFn(data){
		var rankmsg = data.data;
		$(".sign-rank ul").html("");
		if(data.code == "0000"){
			for(var i=0;i<rankmsg.length;i++){
				var rank = $('<li><span class="sign-ranknum">'+rankmsg[i].Score+'</span>'+
							'<span class="sign-rankuser">'+rankmsg[i].Name+'</span></li>');
				rank.appendTo($(".sign-rank ul"));
			}
		}
		else {
			warnTip($(".black-bg"),"获取失败："+data.msg,1000);
		}
	}

	function joinCampaignSucFn(data){
		var bindCode = data.JoinFlag,//是否绑定：0:能参加 1:今日次数已经用完 2:尚未绑定账号
			actID = data.CampaignId,//活动ID
			alreadyCount = data.JoinCount,//已参加次数
			totalCount = data.TotalCount;//总可以参加次数
		if(bindCode == 0){
			//参加活动
			var urlFinishCampaign = wxAddress+"/weixin/campaign/finishCampaign?campaignId="+actID;
			ajaxEncapsulation(urlFinishCampaign, finishCampaignSucFn, "", "GET", "true");
		}else if(bindCode == 1){
			//提示已参加
			warnTip($(".black-bg"),"您已参加活动！",1000);
		}else if(bindCode == 2){
			window.location.href = "bind.html";
		}
	}

	function finishCampaignSucFn(message){
		var result = message.data;
		var fActID = result.CampaignId,//活动ID
			fAlreadyCount = result.JoinCount,//已参加次数
			fTotalCount = result.TotalCount;//总可以参加次数
		if(message.code == "0000"){
			warnTip($(".black-bg"),"参加成功！",1000);
		}else {
			warnTip($(".black-bg"),"参加失败："+data.msg,1000);
		}
	}

	var getSignatureUrl = window.location.href.split('#')[0];
	//分享功能
	var urlGetSignature = wxAddress+"/weixin/sign/getSignature?url="+getSignatureUrl;
	ajaxEncapsulation(urlGetSignature, getSignatureSucFn, "", "GET", "false");

	//选项卡功能
	$(".sign-tip li").on("click", function(){
		$(".sign-main").find(".sign-choice").eq($(this).index(".sign-tip li")).siblings(".sign-choice").removeClass("onShow");
		$(".sign-main").find(".sign-choice").eq($(this).index(".sign-tip li")).addClass("onShow");
		//排行榜
		if($(this).index(".sign-tip li") == 2){
			var urlGetRank = wxAddress+"/weixin/campaign/getRank?page=1&row=10";
			ajaxEncapsulation(urlGetRank, getRankSucFn, "", "GET", "true");
		}
	})

	//判断是否绑定用户，
	$(".sign-cho2").on("click", function(){
		var urlJoinCampaign = wxAddress+"/weixin/campaign/joinCampaign?campaignId="+1;
		ajaxEncapsulation(urlJoinCampaign, joinCampaignSucFn, "", "GET", "true");
	})
	
	$(".sign-focus").on("click", function(){
		window.location.href = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIzMjgyNjgwNw==&scene=124#wechat_redirect";
	})
})